import request from '../utils/request';
import { SERVER_HOST } from '../config';





/**
 用户列表
 **/
export
const get_user_list = query => {
    let token = localStorage.getItem("token")
    if (!!token){
        token=token.replace("\"","").replace("\"","");
    }
    var id =query.id;
    var phone = query.phone;
    var nick_name = query.nick_name;
    var sex = query.sex;
    var status = query.status;
    var data={
        'page':query.page,
        'rows':query.rows,
        'id':id,
        'phone':phone,
        'nick_name':nick_name,
        'sex':sex,
        'status':status
    }
    if (id.length===0){
        delete data.id;
    }if (phone.length===0){
        delete data.phone;
    }if(nick_name.length===0){
        delete data.nick_name;
    }if (sex.length===0){
        delete data.sex;
    }if (status.length===0){
        delete data.status;
    }
    return request({
        url: '/user_info',
        method: 'get',
        headers:{
            Authorization:token
        },
        params: data
    });
};


/**
 编辑用户
**/
export const edit_user = query => {
    switch (query.sex){
        case '男':
            query.sex = 1
            break
        case '女':
            query.sex = 0
            break
        case '保密':
            query.sex = 2
    }
    switch (query.status){
        case '正常':
            query.status = 1
            break
        case '暂停使用':
            query.status = 0
            break
    }
    let token = localStorage.getItem("token")

    if (!!token){
        token=token.replace("\"","").replace("\"","");
    }
    return request({
        url:'/user_info',
        method: 'put',
        headers:{
            Authorization:token
        },
        data:query
    });
};

/**
 编辑用户
 **/
export const add_user = query => {
    switch (query.sex){
        case '男':
            query.sex = 1
            break
        case '女':
            query.sex = 0
            break
        case '保密':
            query.sex = 2
    }
    let token = localStorage.getItem("token")

    if (!!token){
        token=token.replace("\"","").replace("\"","");
    }
    return request({
        url:'/user_info',
        method: 'post',
        headers:{
            Authorization:token
        },
        data:query
    });
};


/**
 删除用户
 **/
export
const delete_user = query => {
    // let token = sessionStorage.getItem('token')
    let token = localStorage.getItem("token")
    if (!!token){
        token=token.replace("\"","");
    }
    var datas={
        id:query.id
    }
    return request({
        url: SERVER_HOST+'/user_info',
        method: 'delete',
        headers:{
            Authorization:token
        },
        data:datas
    });
};


/**
 获取新闻详情
* */
export
const get_news_detail = query => {
    let token = localStorage.getItem("token")
    if (!!token){
        token=token.replace("\"","").replace("\"","");
    }
    var data={
        'news_id':query,
    }
    return request({
        url: '/news_detail',
        method: 'get',
        headers:{
            Authorization:token
        },
        params: data
    });
};


/**
 评论列表查询
 **/
export
const get_comment_info = query => {
    let token = localStorage.getItem("token")
    if (!!token){
        token=token.replace("\"","").replace("\"","");
    }
    var id =query.id;
    var news_id = query.news_id;
    var user_id = query.user_id;
    var content = query.content;
    var status = query.status;
    var data={
        'page':query.page,
        'rows':query.rows,
        'id':id,
        'news_id':news_id,
        'user_id':user_id,
        'content':content,
        'status':status
    }
    if (id.length===0){
        delete data.id;
    }if (news_id.length===0){
        delete data.news_id;
    }if(user_id.length===0){
        delete data.user_id;
    }if (content.length===0){
        delete data.content;
    }if (status.length===0){
        delete data.status;
    }
    return request({
        url: '/comment_info',
        method: 'get',
        headers:{
            Authorization:token
        },
        params: data
    });
};

/**
 评论新增接口
 **/
export const add_comment_info = query => {

    let token = localStorage.getItem("token")

    if (!!token){
        token=token.replace("\"","").replace("\"","");
    }
    return request({
        url:'/comment_info',
        method: 'post',
        headers:{
            Authorization:token
        },
        data:query
    });
};

/**
 编辑评论状态
 **/
export const edit_comment_status = query => {

    let token = localStorage.getItem("token")

    if (!!token){
        token=token.replace("\"","").replace("\"","");
    }
    return request({
        url:'/comment_info',
        method: 'put',
        headers:{
            Authorization:token
        },
        data:query
    });
};

/**
 评论删除接口
 **/
export
const delete_comment = query => {
    // let token = sessionStorage.getItem('token')
    let token = localStorage.getItem("token")
    if (!!token){
        token=token.replace("\"","");
    }
    var datas={
        id:query.id
    }
    return request({
        url: SERVER_HOST+'/comment_info',
        method: 'delete',
        headers:{
            Authorization:token
        },
        data:datas
    });
};


/**
 新闻查询接口
**/
export
const get_news_info = query => {
    let token = localStorage.getItem("token")
    if (!!token){
        token=token.replace("\"","").replace("\"","");
    }
    var id =query.id;
    var news_title = query.news_title;
    var author = query.author;
    var news_type_id = query.news_type_id;
    var is_banner = query.is_banner;
    var status = query.status;
    var data={
        'page':query.page,
        'rows':query.rows,
        'id':id,
        'news_title':news_title,
        'author':author,
        'news_type_id':news_type_id,
        'is_banner':is_banner,
        'status':status
    }
    if (id.length===0){
        delete data.id;
    }if (news_title.length===0){
        delete data.news_title;
    }if(author.length===0){
        delete data.author;
    }if (news_type_id.length===0){
        delete data.news_type_id;
    }if (is_banner.length===0){
        delete data.is_banner;
    }if (status.length===0){
        delete data.status;
    }
    return request({
        url: '/newsinfo',
        method: 'get',
        headers:{
            Authorization:token
        },
        params: data
    });
};


/**
 新闻发布接口
**/
export const add_news_info = query => {

    let token = localStorage.getItem("token")

    if (!!token){
        token=token.replace("\"","").replace("\"","");
    }
    return request({
        url:'/newsinfo',
        method: 'post',
        headers:{
            Authorization:token
        },
        data:query
    });
};

/**
 编辑是否置顶
**/
export const up_banner_api = query => {

    let token = localStorage.getItem("token")

    if (!!token){
        token=token.replace("\"","").replace("\"","");
    }
    return request({
        url:'/news_is_banner',
        method: 'put',
        headers:{
            Authorization:token
        },
        data: {
            is_banner:query
        }
    });
};


/**
 新闻编辑接口
**/
export const edit_news_info = query => {

    let token = localStorage.getItem("token")

    if (!!token){
        token=token.replace("\"","").replace("\"","");
    }
    return request({
        url:'/newsinfo',
        method: 'put',
        headers:{
            Authorization:token
        },
        data:query
    });
};


/**
  新闻删除接口
**/
export
const delete_news = query => {
    // let token = sessionStorage.getItem('token')
    let token = localStorage.getItem("token")
    if (!!token){
        token=token.replace("\"","");
    }
    var datas={
        id:query.id
    }
    return request({
        url: SERVER_HOST+'/newsinfo',
        method: 'delete',
        headers:{
            Authorization:token
        },
        data:datas
    });
};


export
const get_books_info = query => {
    // let token = sessionStorage.getItem('token')
    let token = localStorage.getItem("token")
    if (!!token){
        token=token.replace("\"","").replace("\"","");
    }
    var id =query.id;
    var books_name = query.books_name;
    var author = query.author;
    var classify = query.classify;
    var press = query.press;
    var book_status = query.book_status;
    var data={
        'page':query.page,
        'rows':query.rows,
        'id':id,
        'books_name':books_name,
        'author':author,
        'classify':classify,
        'press':press,
        'book_status':book_status
    }
    if (id.length===0){
        delete data.id;
    }if (books_name.length===0){
        delete data.books_name;
    }if(author.length===0){
        delete data.author;
    }if (classify.length===0){
        delete data.classify;
    }if (press.length===0){
        delete data.press;
    }if (book_status.length===0){
        delete data.book_status;
    }
    return request({
        url: SERVER_HOST+'/bookInfo',
        method: 'get',
        headers:{
            Authorization:token
        },
        params: data
    });
};


// 图书编辑接口
export const edit_books_info = query => {
    // let token = sessionStorage.getItem('token')
    let token = localStorage.getItem("token")

    if (!!token){
        token=token.replace("\"","").replace("\"","");
    }
    return request({
        url:  SERVER_HOST+'/bookInfo',
        method: 'put',
        headers:{
            Authorization:token
        },
        data:query
    });
};

// 图书删除接口
export
const delete_books_info = query => {
    // let token = sessionStorage.getItem('token')
    let token = localStorage.getItem("token")
    if (!!token){
        token=token.replace("\"","");
    }
    var datas={
        id:query.id
    }
    return request({
        url: SERVER_HOST+'/bookInfo',
        method: 'delete',
        headers:{
            Authorization:token
        },
        data:datas
    });
};

// 添加图书接口
export const add_books_info = query => {
    // let token = sessionStorage.getItem('token')
    let token = localStorage.getItem("token")

    if (!!token){
        token=token.replace("\"","").replace("\"","");
    }
    return request({
        url:  SERVER_HOST+'/bookInfo',
        method: 'post',
        headers:{
            Authorization:token
        },
        data:query
    });
};





export
const login = query => {
    // const user = query.user;
    // const pwd = query.pwd;
    return request({
        url: SERVER_HOST+'/login',
        method: 'post',
        data:query
    });
};

export
const register = query => {
    return request({
        url:  SERVER_HOST+'/register/',
        method: 'post',
        data:query
    });
};

export
const getCodes = () => {
    return request({
        url:  SERVER_HOST+'/vercode',
        method: 'get'
    });
};

